package com.yunpeng.aidevice.bms.web.controllers.merchant;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IMerchantBizService;
import com.yunpeng.aidevice.biz.service.IMerchantNoticeMailBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.aidevice.common.dto.cache.LoginUserCacheDTO;
import com.yunpeng.aidevice.common.dto.query.MerchantListQueryDTO;
import com.yunpeng.aidevice.common.dto.save.MailSenderSaveDTO;
import com.yunpeng.aidevice.common.dto.save.MerchantSaveDTO;
import com.yunpeng.aidevice.common.vo.AppUpdateInfoVO;
import com.yunpeng.aidevice.common.vo.NoticeMailConfigVO;
import com.yunpeng.aidevice.common.vo.SimpleEntityVO;
import com.yunpeng.aidevice.common.vo.merchant.MerchantDetailVO;
import com.yunpeng.common.dto.Result;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.util.ParametersCheckUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * MerchantController
 *
 * @author : Moore(hemw)
 * @date : 2020-04-29
 */
@RestController
@RequestMapping("/api/merchant")
public class MerchantController implements Serializable {

    @Resource
    private IMerchantBizService merchantBizService;

    @Resource
    private IMerchantNoticeMailBizService merchantNoticeMailBizService;

    /**
     * 商户端的列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<PagingQueryDTO> list(@RequestBody MerchantListQueryDTO merchantListQueryDTO) {
        ParametersCheckUtil.isNotNull(merchantListQueryDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(merchantListQueryDTO.getPageSize(), "需要传入页数量");
        ParametersCheckUtil.isNotNull(merchantListQueryDTO.getCurrentPage(), "需要传入页码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        merchantListQueryDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return merchantBizService.queryMerchantList(merchantListQueryDTO);
    }

    /**
     * 商户详情
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public Result<MerchantDetailVO> detail(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        ParametersCheckUtil.isNotBlank(merchantCode, "无效的商户代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.merchantDetail(merchantCode, loginUserCacheDTO.getTenantCode());
    }

    /**
     * 用户用户所属的商户详情
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "loginUserMerchantDetail", method = RequestMethod.POST)
    public Result<MerchantDetailVO> loginUserMerchantDetail() {
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.merchantDetail(loginUserCacheDTO.getMerchantCode(), loginUserCacheDTO.getTenantCode());
    }

    /**
     * 新增商户
     *
     * @param merchantSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNew", method = RequestMethod.POST)
    public Result<Void> addNew(@RequestBody MerchantSaveDTO merchantSaveDTO) {
        ParametersCheckUtil.isNotNull(merchantSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(merchantSaveDTO.getMerchantNumber(), "需要传入商户编号");
        ParametersCheckUtil.isNotNull(merchantSaveDTO.getMerchantName(), "需要传入商户名称");
        ParametersCheckUtil.isNotNull(merchantSaveDTO.getDisabled(), "设备需要选择禁用或者启用");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        merchantSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return merchantBizService.addNewMerchant(merchantSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 商户更新
     *
     * @param merchantSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public Result<Void> modify(@RequestBody MerchantSaveDTO merchantSaveDTO) {
        ParametersCheckUtil.isNotNull(merchantSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotNull(merchantSaveDTO.getMerchantNumber(), "需要传入商户编号");
        ParametersCheckUtil.isNotNull(merchantSaveDTO.getMerchantCode(), "需要传入商户代码");
        ParametersCheckUtil.isNotNull(merchantSaveDTO.getMerchantName(), "需要传入商户名称");
        ParametersCheckUtil.isNotNull(merchantSaveDTO.getDisabled(), "设备需要选择禁用或者启用");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        merchantSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return merchantBizService.updateMerchant(merchantSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 更新商户失效时间
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "invalidDateUpdate", method = RequestMethod.POST)
    public Result<Void> invalidDateUpdate(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        Date invalidDate = requestParam.getDate("invalidDate");
        ParametersCheckUtil.isNotBlank(merchantCode, "无效的商户代码");
        ParametersCheckUtil.isNotNull(invalidDate, "无效的失效时间");
        Instant instant = invalidDate.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.invalidDateUpdate(merchantCode, loginUserCacheDTO.getTenantCode(), localDateTime, loginUserCacheDTO.getCode());
    }

    /**
     * 商户下拉菜单
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "dropDownList", method = RequestMethod.POST)
    public Result<List<SimpleEntityVO>> dropDownList() {
        return merchantBizService.dropDownList(ConfigConstantValues.getLoginUserCacheDTO().getTenantCode());
    }

    /**
     * 商户固定统计信息
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMerchantCountValue", method = RequestMethod.POST)
    public Result<JSONArray> getMerchantCountValue(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        JSONArray merchantCodes = requestParam.getJSONArray("merchantCodes");
        ParametersCheckUtil.collectionIsNotNullOrderEmpty(merchantCodes, "必须传入商户代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.getMerchantCountValue(merchantCodes.toJavaList(String.class), loginUserCacheDTO.getTenantCode());
    }

    /**
     * 启用商户
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public Result<Void> enable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        ParametersCheckUtil.isNotBlank(merchantCode, "无效的商户代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.enableDisableMerchant(false, merchantCode, loginUserCacheDTO.getTenantCode(), loginUserCacheDTO.getCode());
    }

    /**
     * 禁用商户
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public Result<Void> disable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        ParametersCheckUtil.isNotBlank(merchantCode, "无效的商户代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.enableDisableMerchant(true, merchantCode, loginUserCacheDTO.getTenantCode(), loginUserCacheDTO.getCode());
    }

    /**
     * 获取默认的邮箱提醒发送配置
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getDefaultNoticeMailConfig", method = RequestMethod.POST)
    public Result<NoticeMailConfigVO> getDefaultNoticeMailConfig() {
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantNoticeMailBizService.getDefaultMailConfig(loginUserCacheDTO.getTenantCode());
    }

    /**
     * 获取商户的邮箱提醒发送配置
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMerchantNoticeMailConfig", method = RequestMethod.POST)
    public Result<NoticeMailConfigVO> getMerchantNoticeMailConfig(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        ParametersCheckUtil.isNotBlank(merchantCode, "无效的商户代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantNoticeMailBizService.getMerchantMailConfig(merchantCode, loginUserCacheDTO.getTenantCode());
    }

    /**
     * 新增提醒邮箱配置
     *
     * @param mailSenderSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNewNoticeMailConfig", method = RequestMethod.POST)
    public Result<Void> addNewNoticeMailConfig(@RequestBody MailSenderSaveDTO mailSenderSaveDTO) {
        ParametersCheckUtil.isNotNull(mailSenderSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getMailHost(), "邮箱服务器地址不能为空");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getPassword(), "密码不能为空");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getSenderEmail(), "发送邮箱不能为空");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getSenderName(), "发送人不能为空");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        mailSenderSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return merchantNoticeMailBizService.addNewConfig(mailSenderSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 更新提醒邮箱配置
     *
     * @param mailSenderSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateNoticeMailConfig", method = RequestMethod.POST)
    public Result<Void> updateNoticeMailConfig(@RequestBody MailSenderSaveDTO mailSenderSaveDTO) {
        ParametersCheckUtil.isNotNull(mailSenderSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getMailHost(), "邮箱服务器地址不能为空");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getPassword(), "密码不能为空");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getSenderEmail(), "发送邮箱不能为空");
        ParametersCheckUtil.isNotBlank(mailSenderSaveDTO.getSenderName(), "发送人不能为空");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        mailSenderSaveDTO.setTenantCode(loginUserCacheDTO.getTenantCode());
        return merchantNoticeMailBizService.updateMailSender(mailSenderSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 设定商户更新app配置
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateAppUpdateTime", method = RequestMethod.POST)
    public Result<Void> updateAppUpdateTime(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        Integer startTime = requestParam.getInteger("startTime");
        Integer endTime = requestParam.getInteger("endTime");
        ParametersCheckUtil.isNotBlank(merchantCode, "无效的商户代码");
        ParametersCheckUtil.isGreatOrEqualsThenZero(startTime, "无效的开始时间");
        ParametersCheckUtil.isGreatOrEqualsThenZero(endTime, "无效的结束时间");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.updateAppUpdateTime(startTime.shortValue(), endTime.shortValue(), merchantCode, loginUserCacheDTO.getTenantCode(), loginUserCacheDTO.getCode());
    }


    /**
     * 获取商户更新app配置
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAppUpdateInfo", method = RequestMethod.POST)
    public Result<AppUpdateInfoVO> getAppUpdateInfo(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        ParametersCheckUtil.isNotBlank(merchantCode, "无效的商户代码");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return merchantBizService.getMerchantAppUpdateInfo(merchantCode, loginUserCacheDTO.getTenantCode());
    }
}
